<?php

namespace app\common\model;
use \think\Model;
use OSS\OssClient;
use OSS\Core\OssException;

//var_dump(__DIR__ . '/../../vendor/OSS/OssClient.php');
//var_dump(file_exists(__DIR__ . '/../../vendor/OSS/OssClient.php'));die;
require_once(dirname(dirname(__DIR__)) . '/vendor/OSS/OssClient.php');
require_once(dirname(dirname(__DIR__)) . '/vendor/OSS/Core/OssException.php');


/**
 * This is the model class for table "article".
 *
 * @property integer $id
 * @property string $name
 * @property string $intro
 * @property integer $sort
 * @property integer $status
 * @property integer $article_category_id
 * @property integer $create_time
 * @property string $logo
 * @property integer $views
 * @property string $source
 */
class UALiUp 
{
    private $accessKeyId = 'LTAIPAXQFTTSO8Sf';
    private $accessKeySecret = 'CLKU2C77NDFBxvTzBRWinL6QaCSrLa';
    private $endpoint = 'oss-cn-beijing.aliyuncs.com';
    //private $endpoint = 'oss-cn-beijing-internal.aliyuncs.com';
    private $bucket= "txrd";
    
    //阿里云OSS 云存储
    public function oss_up($file, $object,$type = 1){
        if($type == 1){
            $info = getimagesize($file['tmp_name']);
            $type = explode('/', $info['mime']);
            $type = $type[1];
        }
        //var_dump($file);die;

        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;

        // 存储空间名称
        $bucket= $this->bucket;
        // 文件名称
        //$thumb = 'head_img/head_thumb_img'.time().mt_rand(10000,99999).".".$type;
        //echo $object;die;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        $filePath = $file['tmp_name'];
        $img = '';
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ret = $ossClient->uploadFile($bucket, $object, $filePath);
            /*$options = array(
                OssClient::OSS_FILE_DOWNLOAD => $thumb,
                OssClient::OSS_PROCESS => "image/resize,lfit,h_100,w_100" 
            );
            $res = $ossClient->getObject($bucket, $object, $options);*/
            
            //var_dump($ret);die;
            $img = $ret['info']['url'];
            //echo "<pre>";
            //$sql = "select * from sys_set where `key` = 'img_http'";
            //$img_http = \Yii::$app->db->createCommand($sql)->queryOne();
            $img_http = "img.oxiong.com";
            //$sql = "select * from sys_set where `key` = 'OSS_URL'";
            //$oss_url = \Yii::$app->db->createCommand($sql)->queryOne();
            $oss_url = "txrd.oss-cn-beijing.aliyuncs.com";
            $img = str_replace($oss_url, $img_http, $img);
            //var_dump($img);
            return $img;
        } catch(OssException $e) {
            //var_dump($e->getMessage());die;
            return false;
        }
    }
    
    //阿里云OSS 云存储
    public function oss_catch_up($filePath, $object,$type = 1){
        

        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;

        // 存储空间名称
        $bucket= $this->bucket;
        // 文件名称
        //$thumb = 'head_img/head_thumb_img'.time().mt_rand(10000,99999).".".$type;
        //echo $object;die;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        //$filePath = $file['tmp_name'];
        //var_dump($filePath);die;
        $img = '';
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ret = $ossClient->uploadFile($bucket, $object, $filePath);
            /*$options = array(
                OssClient::OSS_FILE_DOWNLOAD => $thumb,
                OssClient::OSS_PROCESS => "image/resize,lfit,h_100,w_100" 
            );
            $res = $ossClient->getObject($bucket, $object, $options);*/
            
            //var_dump($ret);die;
            $img = $ret['info']['url'];
            //echo "<pre>";
            //$sql = "select * from sys_set where `key` = 'img_http'";
            //$img_http = \Yii::$app->db->createCommand($sql)->queryOne();
            $img_http = "img.oxiong.com";
            //$sql = "select * from sys_set where `key` = 'OSS_URL'";
            //$oss_url = \Yii::$app->db->createCommand($sql)->queryOne();
            $oss_url = "txrd.oss-cn-beijing.aliyuncs.com";
            $img = str_replace($oss_url, $img_http, $img);
            //echo "<pre>";
            //echo 1;die;
            return $img;
        } catch(OssException $e) {
            //echo 2;die;
            //var_dump($e->getMessage());die;
            return false;
        }
    }
}
